//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PaymentTerm.h"
using namespace Cephei::QL::Experimental::Commodities;
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (String^ name, QL::Experimental::Commodities::PaymentTerm::EventTypeEnum eventType, Int32 offsetDays, Cephei::QL::Times::ICalendar^ calendar) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CCalendar^ _Ccalendar;
    try
    {
#ifdef HANDLE
        _phPaymentTerm = NULL;
#endif
        std::string _name = (std::string)ValueHelper::Convert (name);
        QuantLib::PaymentTerm::EventType _eventType = (QuantLib::PaymentTerm::EventType)eventType ;
        QuantLib::Integer _offsetDays = (QuantLib::Integer)ValueHelper::Convert (offsetDays);
        _Ccalendar = safe_cast<CCalendar^> (calendar);
        _Ccalendar->Lock();
        QuantLib::Calendar& _calendar = static_cast<QuantLib::Calendar&> (_Ccalendar->GetReference ()); 
        _ppPaymentTerm = new boost::shared_ptr<QuantLib::PaymentTerm> (new QuantLib::PaymentTerm ( _name,  _eventType,  _offsetDays,  _calendar ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccalendar != nullptr) _Ccalendar->Unlock();
    }
}
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phPaymentTerm = NULL;
#endif
        _ppPaymentTerm = new boost::shared_ptr<QuantLib::PaymentTerm> (new QuantLib::PaymentTerm ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (boost::shared_ptr<QuantLib::PaymentTerm>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPaymentTerm = NULL;
#endif
	_ppPaymentTerm = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (QuantLib::PaymentTerm& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPaymentTerm = NULL;
#endif
	_ppPaymentTerm = new boost::shared_ptr<QuantLib::PaymentTerm> (&childNative);
    
    _PaymentTermOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (CPaymentTerm^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPaymentTerm = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPaymentTerm = new boost::shared_ptr<QuantLib::PaymentTerm> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPaymentTerm = NULL;
#endif
	if (!t->IsSubclassOf(CPaymentTerm::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (QuantLib::Handle<QuantLib::PaymentTerm>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPaymentTerm = &childNative;
	_ppPaymentTerm = &static_cast<boost::shared_ptr<QuantLib::PaymentTerm>>(childNative.currentLink());
    
    _PaymentTermOwner = owner;
}
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (QuantLib::Handle<QuantLib::PaymentTerm> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPaymentTerm = &childNative;
	_ppPaymentTerm = &static_cast<boost::shared_ptr<QuantLib::PaymentTerm>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CPaymentTerm::CPaymentTerm (QuantLib::PaymentTerm childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPaymentTerm = NULL;
#endif
	_ppPaymentTerm = new boost::shared_ptr<QuantLib::PaymentTerm> (new QuantLib::PaymentTerm (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CPaymentTerm::~CPaymentTerm ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPaymentTerm != NULL)
    {
	    delete _ppPaymentTerm;
        _ppPaymentTerm = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CPaymentTerm::!CPaymentTerm ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPaymentTerm != NULL)
    {
	    delete _ppPaymentTerm;
    }
}
QuantLib::PaymentTerm& Cephei::QL::Experimental::Commodities::CPaymentTerm::GetReference ()
{
    if (_ppPaymentTerm == NULL) throw gcnew NativeNullException ();
	return **_ppPaymentTerm;
}
boost::shared_ptr<QuantLib::PaymentTerm>& Cephei::QL::Experimental::Commodities::CPaymentTerm::GetShared ()
{
    if (_ppPaymentTerm == NULL) throw gcnew NativeNullException ();
	return *_ppPaymentTerm;
}
QuantLib::PaymentTerm* Cephei::QL::Experimental::Commodities::CPaymentTerm::GetPointer ()
{
    if (_ppPaymentTerm == NULL) throw gcnew NativeNullException ();
	return &**_ppPaymentTerm;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PaymentTerm>& Cephei::QL::Experimental::Commodities::CPaymentTerm::GetHandle ()
{
	if (_phPaymentTerm == NULL)
	{
		_phPaymentTerm = new Handle<QuantLib::PaymentTerm> (*_ppPaymentTerm);
	}
	return *_phPaymentTerm;
}
#endif
bool Cephei::QL::Experimental::Commodities::CPaymentTerm::HasNative () 
{
	return (_ppPaymentTerm != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Experimental::Commodities::CPaymentTerm::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar& _rv = (QuantLib::Calendar&)(*_ppPaymentTerm)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = gcnew Cephei::QL::Times::CCalendar (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Commodities::CPaymentTerm::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppPaymentTerm)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Experimental::Commodities::PaymentTerm::EventTypeEnum Cephei::QL::Experimental::Commodities::CPaymentTerm::EventType::get ()
{
    try
    {
    	QuantLib::PaymentTerm::EventType _rv = (QuantLib::PaymentTerm::EventType)(*_ppPaymentTerm)->eventType ( );   
        QL::Experimental::Commodities::PaymentTerm::EventTypeEnum _nrv = (QL::Experimental::Commodities::PaymentTerm::EventTypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Experimental::Commodities::CPaymentTerm::GetPaymentDate (DateTime date)
{
    try
    {
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date);
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppPaymentTerm)->getPaymentDate ( _date );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Experimental::Commodities::CPaymentTerm::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppPaymentTerm)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Int32 Cephei::QL::Experimental::Commodities::CPaymentTerm::OffsetDays::get ()
{
    try
    {
    	QuantLib::Integer _rv = (QuantLib::Integer)(*_ppPaymentTerm)->offsetDays ( );   
        Int32 _nrv = (Int32)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::IPaymentTerm^ Cephei::QL::Experimental::Commodities::CPaymentTerm_Factory::Create (String^ name, QL::Experimental::Commodities::PaymentTerm::EventTypeEnum eventType, Int32 offsetDays, Cephei::QL::Times::ICalendar^ calendar)
{
    return gcnew CPaymentTerm ( name,  eventType,  offsetDays,  calendar);
}
Cephei::QL::Experimental::Commodities::IPaymentTerm^ Cephei::QL::Experimental::Commodities::CPaymentTerm_Factory::Create ()
{
    return gcnew CPaymentTerm ();
}
